package org.mapki;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;

import android.app.Activity;
import android.content.Intent;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Environment;
import android.os.storage.OnObbStateChangeListener;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
	private OnClickListener buttonListener1;
	private OnClickListener buttonListener2;
	private OnClickListener buttonListener3;
	private OnClickListener buttonListener4;
	private ImageAdapter imgAdapter;
	
	
	
	private static String APP_DIR =Environment.getExternalStorageDirectory().getAbsolutePath()+File.separator+"Mapki";
	
	LocationManager locationManager;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.main);
    	Log.i("MainActivity.onCreate", "APP_DIR: "+APP_DIR);
    	try{
    		addButtons();
    	}catch(Exception ex){
    		Toast.makeText(getBaseContext(), "Brak danych programu w katalogu: "+APP_DIR, Toast.LENGTH_SHORT).show();
			Log.e("MapsActivity.loadMapFile", "Exception during loading data from directory: "+APP_DIR,ex);
    	}
    	/*
        buttonListener1 = new OnClickListener() {
            @Override
        	public void onClick(View v) {
                setSelectedTrasa(1);
              }
        };
        buttonListener2 = new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		setSelectedTrasa(2);
        	}
        };
        buttonListener3 = new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		setSelectedTrasa(3);
        	}
        };
        buttonListener4 = new OnClickListener() {
        	@Override
        	public void onClick(View v) {
        		setSelectedTrasa(4);
        	}
        };
        
        Button b1 =(Button) findViewById(R.id.Trasa01);
        b1.setOnClickListener(buttonListener1);
        Button b2 =(Button) findViewById(R.id.Trasa02);
        b2.setOnClickListener(buttonListener2);
        Button b3 =(Button) findViewById(R.id.Trasa03);
        b3.setOnClickListener(buttonListener3);
        Button b4 =(Button) findViewById(R.id.Trasa04);
        b4.setOnClickListener(buttonListener4);
        */
        //imgAdapter = new ImageAdapter(this.getBaseContext());
        
    }
    @Override
    public void onBackPressed() {
       finish();
       System.exit(0);
       return;
    }
    
//    @Override
//	public boolean onKeyDown(int keyCode, KeyEvent event) {
//		Log.i("DescriptionActivity.onKeyDown",
//				"method started with attribute: " + keyCode);
//		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
//			Log.i("MainActivity.onKeydown", "Received key back");
//			System.exit(0);
//			return true;
//		}
//
//		return super.onKeyDown(keyCode, event);
//	}
    private void addButtons()throws Exception {
    	File appDirectory = new File(APP_DIR);
    	if(!appDirectory.exists() || !appDirectory.isDirectory() || appDirectory.listFiles().length == 0){
    		throw new Exception("Exception during loading data from directory");
    	}
    	File[] routeTab = appDirectory.listFiles();
    	Arrays.sort(routeTab, new Comparator<File>() {
    		@Override
			public int compare(File object1, File object2) {
				return object1.getName().compareTo(object2.getName());
			}
		});
    	LinearLayout linearLayout = (LinearLayout)findViewById(R.id.LinLayout);
        for(File file: routeTab){
        	if(file.isDirectory()){
        		Button btn = new Button(this); 
	        	btn.setText(file.getName());
	        	btn.setOnClickListener(new ButtonClickListener(file.getAbsolutePath()));
	        	linearLayout.addView(btn); 

        	}
        }
    }
    
    
    private void buttonSelected(String path){
    	Bundle b = new Bundle();
    	b.putString("route_dir",path);
    	Intent i = new Intent(this,DescriptionActivity.class);
    	i.putExtras(b);
    	startActivity(i);
    }
    /*
    private void setSelectedTrasa(int trasa){
    	Bundle b = new Bundle();
    	switch(trasa){
    	case 1:
    		//imgAdapter.setImagesSet(imgSet1);
    		b.putIntArray("trasa_img", imgSet1);
    		b.putString("trasa_desc", "Testowy opis trasy\n\n To jest test.To jest test.To jest test.To jest test.To jest test.To jest test.To jest test.To jest test.To jest test.To jest test.To jest test.To jest test.To jest test.To jest test.");
    		b.putString("trasa_path", "/sdcard/Mapki/trasa0/gpx");
    		break;
    	case 2:
    		b.putIntArray("trasa_img", imgSet2);
    		b.putString("trasa_desc", "desc2");
    		b.putString("trasa_path", "trasa2.txt");
    		break;
    	case 3:
    		b.putIntArray("trasa_img", imgSet3);
    		b.putString("trasa_desc", "desc3");
    		b.putString("trasa_path", "trasa3.txt");
    		break;
    	case 4:
    		b.putIntArray("trasa_img", imgSet4);
    		b.putString("trasa_desc", "desc4");
    		b.putString("trasa_path", "tras3.txt");
    		break;
    	default : Log.e("MainActivity.setSelectedTrasa", "Unknown trasa number");
    	}
    	
    	Intent i = new Intent(this,DescriptionActivity.class);
    	i.putExtras(b);
    	startActivity(i);
    	
//    	setContentView(R.layout.map_desc);
//    	LayoutInflater inflater = (LayoutInflater)this.getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//    	inflater.inflate(R.layout.map_desc,null);
//    	
    	
    	//currentLayout = R.layout.map_desc;
    	
        
    }
    */
    class ButtonClickListener implements OnClickListener{
		private String routePath;
		ButtonClickListener(String routePath){
			this.routePath = routePath;
		}
		@Override
		public void onClick(View v) {
			buttonSelected(routePath);
		}
	}
     
}